package com.edu.springboot_disk_erban.controller;

import com.edu.springboot_disk_erban.dao.FileDao;
import com.edu.springboot_disk_erban.entity.FileBean;
import com.edu.springboot_disk_erban.entity.ResultModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestParam;


import javax.annotation.Resource;
import java.util.List;

@Controller
public class TypeController {

    @Resource
    FileDao fileDao;
    /**
     * 音乐 musicLists?filepath=
     */
    @RequestMapping("/musicLists")
    @ResponseBody
    public ResultModel<List<FileBean>> getMusicLists(@RequestParam String filepath) {
        List<FileBean> fileBeans = fileDao.findBySonPath(filepath);

        for (int i = 0; i < fileBeans.size(); i++) {
            //移除的就是多的哪一个
            String substring = fileBeans.get(i).getFileName().substring(fileBeans.get(i).getFileName().lastIndexOf(".") + 1);
            //移除   /wode/aaa/a.jpg    /wode/aaa/bbb/svd
            if (!substring.equals("mp3") || substring.equals("wma") || substring.equals("midi") ||substring.equals("MOV")) {
                fileBeans.remove(fileBeans.get(i));
                i--;
            }
        }
        //
        ResultModel<List<FileBean>> resultModel;
        if (fileBeans != null) {
            resultModel = ResultModel.success("查询列表成功！！！", fileBeans, fileBeans.size());
        } else {
            resultModel = ResultModel.error("查询列表失败！！！");
        }
        return resultModel;
    }

    /**
     * 视频 videoLists?filepath
     *
     * @return
     */
    @RequestMapping("/videoLists")
    @ResponseBody
    public ResultModel<List<FileBean>> getVideoLists(@RequestParam String filepath) {
        List<FileBean> fileBeans = fileDao.findBySonPath(filepath);
        for (int i = 0; i < fileBeans.size(); i++) {
            //移除的就是多的哪一个
            String substring = fileBeans.get(i).getFileName().substring(fileBeans.get(i).getFileName().lastIndexOf(".") + 1);
            //移除   /wode/aaa/a.jpg    /wode/aaa/bbb/svd
            if (!(substring.equals("mp4") || substring.equals("avi") || substring.equals("mov")
                    || substring.equals("rmvb") || substring.equals("asf") || substring.equals("rm") || substring.equals("rm")
                    || substring.equals("navi") || substring.equals("mkv") || substring.equals("webm"))) {
                fileBeans.remove(fileBeans.get(i));
                i--;
            }
        }

        //
        ResultModel<List<FileBean>> resultModel;
        if (fileBeans != null) {
            resultModel = ResultModel.success("查询列表成功！！！", fileBeans, fileBeans.size());
        } else {
            resultModel = ResultModel.error("查询列表失败！！！");
        }
        return resultModel;
    }

    /**
     * 文档  documentLists?filepath
     *
     * @return
     */
    @RequestMapping("/documentLists")
    @ResponseBody
    public ResultModel<List<FileBean>> getDocumentLists(@RequestParam String filepath) {
        List<FileBean> fileBeans = fileDao.findBySonPath(filepath);
        for (int i = 0; i < fileBeans.size(); i++) {
            //移除的就是多的哪一个
            String substring = fileBeans
                    .get(i).getFileName()
                    .substring(fileBeans.get(i).getFileName().lastIndexOf(".") + 1);
            //移除   /wode/aaa/a.jpg    /wode/aaa/bbb/svd
            if (!(substring.equals("doc") || substring.equals("docx") ||
                    substring.equals("xls") || substring.equals("xlsx") ||
                    substring.equals("pdf") || substring.equals("xmind") || substring.equals("txt") || substring.equals("pptx")
            )) {
                fileBeans.remove(fileBeans.get(i));
                i--;
            }
        }
        //
        ResultModel<List<FileBean>> resultModel;
        if (fileBeans != null) {
            resultModel = ResultModel.success("查询列表成功！！！", fileBeans, fileBeans.size());
        } else {
            resultModel = ResultModel.error("查询列表失败！！！");
        }
        return resultModel;
    }

    /**
     * 图片 pictureLists?filepath
     *
     * @return
     */
    @RequestMapping("/pictureLists")
    @ResponseBody
    public ResultModel<List<FileBean>> getPictureLists(@RequestParam String filepath) {
        List<FileBean> fileBeans = fileDao.findBySonPath(filepath);
        for (int i = 0; i < fileBeans.size(); i++) {
            //移除的就是多的哪一个
            String substring = fileBeans.get(i).getFileName().substring(fileBeans.get(i).getFileName().lastIndexOf(".") + 1);
            //移除   /wode/aaa/a.jpg    /wode/aaa/bbb/svd
            if (!(substring.equals("jpg") || substring.equals("png") || substring.equals("jpeg") || substring.equals("ico")
                    || substring.equals("pic") || substring.equals("jpeg") || substring.equals("webp")
                    || substring.equals("svg") || substring.equals("vsd"))) {
                fileBeans.remove(fileBeans.get(i));
                i--;
            }
        }
        //
        ResultModel<List<FileBean>> resultModel;
        if (fileBeans != null) {
            resultModel = ResultModel.success("查询列表成功！！！", fileBeans, fileBeans.size());
        } else {
            resultModel = ResultModel.error("查询列表失败！！！");
        }
        return resultModel;
    }

}
